package dao.impl;

import dao.IOrderStatusDao;
import jdbc_util.FirstApp;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import pojo.OrderStatus;

import java.sql.SQLException;
import java.util.List;

public class OrderStatusDaoImpl implements IOrderStatusDao {
    @Override
    public void addMyOrderStatus(OrderStatus orderStatus) {
        QueryRunner queryRunner = new QueryRunner();
        try {
            String sql = "insert into orderstatus(orderstatusname) values (?)";
            queryRunner.update(FirstApp.getConn(), sql, orderStatus.getOrderStatusName());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateMyOrderStatus(OrderStatus orderStatus) {
        QueryRunner queryRunner = new QueryRunner();
        try {
            String sql = "update orderstatus set orderstatusname = ? where orderstatusid = ? ";
            queryRunner.update(FirstApp.getConn(), sql, orderStatus.getOrderStatusName(), orderStatus.getOrderStatusid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteMyOrderStatus(int id) {
        QueryRunner queryRunner = new QueryRunner();
        try {
            String sql = "delete from orderstatus where orderstatusid = ?";
            queryRunner.update(FirstApp.getConn(), sql, id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public OrderStatus findOrderStatusById(int id) {
        QueryRunner queryRunner = new QueryRunner();
        OrderStatus orderStatus = null;
        try {
            String sql = "select * from orderstatus where orderstatusid = ?";
            orderStatus = queryRunner.query(FirstApp.getConn(), sql, new BeanHandler<>(OrderStatus.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return orderStatus;
    }

    @Override
    public List<OrderStatus> findAllOrderStatus() {
        QueryRunner queryRunner = new QueryRunner();
        List<OrderStatus> orderStatusList = null;
        try {
            String sql = "select * from orderstatus";
            orderStatusList = queryRunner.query(FirstApp.getConn(), sql, new BeanListHandler<>(OrderStatus.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return orderStatusList;
    }
}
